package club.framework.web.application.sys.resource.controller;

import club.framework.web.application.sys.resource.entity.Resource;
import club.framework.web.application.sys.resource.service.IResourceService;
import club.framework.web.base.entity.BasePageReqEntity;
import club.framework.web.base.entity.Result;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @date 2017/9/29.
 * @Email:sunmch@163.com
 * @author: 念梓
 * des:
 */
@Controller
@RequestMapping("sys/resource")
public class ResourceController {

    private static final Logger log = LogManager.getLogger(ResourceController.class);

    @Autowired
    private IResourceService resourceService;

    @RequestMapping("index")
    public String index(){
        return "application/sys/resource/index";
    }


    @RequestMapping("add")
    @ResponseBody
    public Object add(Resource resource){
        Result result = new Result();
        try{
            resourceService.save(resource);
        }catch (Exception ex){
            log.error("保存权限: {},失败: {}",resource,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public Object update(Resource resource){
        Result result = new Result();
        try{
            resourceService.update(resource);
        }catch (Exception ex){
            log.error("修改权限信息: {},失败：{}",resource,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("deleteresourcebyid")
    @ResponseBody
    public Object deleteResourceById(int id){
        Result result = new Result();
        try{
            resourceService.delete(id);
        }catch (Exception ex){
            log.error("删除权限id:{},删除失败: {}",id,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("loadpage")
    @ResponseBody
    public Object loadPage(@RequestBody BasePageReqEntity<Resource> basePageReqEntity){
        return resourceService.queryPage(basePageReqEntity);
    }

    @RequestMapping("getresourcebyid")
    @ResponseBody
    public Object getResourceById(int id){
        Result result = new Result();
        try{
            result.setData(resourceService.getObject(id));
        }catch (Exception ex){
            log.error("根据权限id:{},获取权限信息失败: {}",id,ex.getMessage());
            result.setMsg(ex.getMessage());
        }
        return result;
    }

}
